package com.crm.controller.before_sale;

import com.crm.pojo.Scheme;
import com.crm.service.before_sale.SchemeService;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解决方案
 */
@Controller
public class SchemeController {
    @Autowired
    private SchemeService schemeService;
    //解决方案表
    @GetMapping("zjk-scheme")
    public String toSchemeDatagrid(){ return "before-sale/scheme";}
    //添加解决方案表单
    @GetMapping("zjk-scheme-form")
    public String toSchemeFrom(){
        return "before-sale/scheme-form";
    }
    //编辑解决方案表单
    @GetMapping("zjk-edit-scheme-form")
    public String toEditSchemeFrom(){
        return "before-sale/edit-scheme-form";
    }
    //解决方案详情表单（仅查看）
    @GetMapping("zjk-scheme-details")
    public String toSchemeDetails(){
        return "before-sale/scheme-details";
    }

    /**
     * 分页展示解决方案列表
     * @param scheme
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("showSchemeList")
    @ResponseBody
    public Map showSchemeList(Scheme scheme,int page, int rows, HttpServletRequest request){
        List<Scheme> list = schemeService.showSchemeList(scheme,page,rows,null,null);
        Long total = schemeService.getSchemeCount(scheme,null,null);
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return map;
    }

    /**
     * 解决方案高级查询
     * @param scheme
     * @param page
     * @param rows
     * @param submitStartTime
     * @param submitEndTime
     * @return
     */
    @PostMapping("advance-scheme")
    @ResponseBody
    public Map advanceScheme(Scheme scheme,int page,int rows,Date submitStartTime, Date submitEndTime){
        Long total = schemeService.getSchemeCount(scheme,submitStartTime,submitEndTime);
        List<Scheme> list = schemeService.showSchemeList(scheme,page,rows,submitStartTime,submitEndTime);
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return map;
    }

    /**
     * 删除解决方案
     * @param scId
     * @return
     */
    @PostMapping("deleteScheme")
    @ResponseBody
    public Result deleteScheme(Long scId){
        Long id = scId == null ? 0:new Long(scId);
        return schemeService.deleteScheme(scId);
    }

    /**
     * 添加一个解决方案
     * @param scTheme
     * @param clientId
     * @param chanceId
     * @param chragePeopleId
     * @param submitTime
     * @param scCheck
     * @param clientInfoFeedback
     * @param schemeContent
     * @return
     */
    @PostMapping("addScheme")
    @ResponseBody
    public Result addScheme(String scTheme, Long clientId, Long chanceId, Long chragePeopleId,
                            Date submitTime, String scCheck, String clientInfoFeedback, String schemeContent){
        try {
            return schemeService.addScheme(scTheme,clientId,chanceId,chragePeopleId,submitTime,scCheck,clientInfoFeedback,schemeContent);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);
        }

    }

    /**
     * 编辑
     * @param scheme
     * @return
     */
    @PostMapping("editScheme")
    @ResponseBody
    public Result editScheme(Scheme scheme){
        return schemeService.editScheme(scheme);
    }





}
